package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import model.User;
import util.SocketUtil;
import util.StateCode;

public class InfoPanel extends JPanel implements Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DefaultTableModel dtm;
	private JLabel player;
	private JTextArea chat;
	public InfoPanel() {
		setBackground(Color.yellow);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		JPanel top=new JPanel();
		top.setLayout(new GridBagLayout());
		GridBagConstraints gbc=new GridBagConstraints();
		JLabel label1=new JLabel();		
		label1.setIcon(new ImageIcon(getClass().getResource("../image/photo1.png")));
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.insets=new Insets(20, 0, 10, 0);
		top.add(label1,gbc);
		player=new JLabel("玩家x");
		gbc.gridy=1;
		top.add(player,gbc);
		add(top);
		
		JPanel middle=new JPanel();
		middle.setLayout(new BorderLayout());
		dtm=new DefaultTableModel(new Object[][]{},new String[]{"昵称","IP","联机时间"});
		JTable table=new JTable(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane sp=new JScrollPane(table);
		sp.setPreferredSize(new Dimension(230, 200));
		middle.add(sp);
		add(middle);
		
		JPanel bottom=new JPanel();
		bottom.setLayout(new BorderLayout());
		chat=new JTextArea();
		chat.setPreferredSize(new Dimension(200, 500));
		JPanel panel=new JPanel();
		panel.setLayout(new BorderLayout());
		JTextField tf=new JTextField();
		JButton send=new JButton("发送");
		send.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String sms=SocketUtil.getUser().getName()+":"+tf.getText()+"\n";
				chat.append(sms);
				SocketUtil.sendMessage(sms);
				tf.setText("");
			}  
		});
		panel.add(tf); panel.add(send,BorderLayout.EAST);
		bottom.add(chat); bottom.add(panel,BorderLayout.SOUTH);
		add(bottom);
		
	}
	@Override
	public void update(Observable o, Object arg) {
		if(arg instanceof User){
			User user=(User) arg;
			dtm.addRow(new Object[]{user.getName(),user.getIp(),user.getLoginTime()});
			if(o.equals(user)) player.setText(user.getName());
		}
		else if(arg instanceof String){
			String s=(String) arg;
			chat.append(s);
		}
		else if(arg instanceof Integer){
			switch((int)arg){
			case StateCode.LINKRESET:
				for(int i=0;i<dtm.getRowCount();i++)
					dtm.removeRow(i);
				break;
			}
		}
	}
}
